package com.topfinance.cnaps2.web.sys;

import java.util.HashMap;
import java.util.List;

import com.topfinance.cnaps2.ebo.SysFuncsEbo;
import com.topfinance.cnaps2.service.ISysFuncManager;
import com.topfinance.frame.util.StringUtil;
import com.topfinance.frame.web.BaseSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author
 * @version 1.50
 *          <p>
 *          Copyright: Copyright (c) 2004 2005
 *          </p>
 *          <p>
 *          Company:
 *          </p>
 */
public final class SysFuncAction extends BaseSupport implements ModelDriven<SysFuncsEbo> {

	private ISysFuncManager funcManager = null;

	private SysFuncsEbo funcEbo = new SysFuncsEbo();

	public SysFuncsEbo getModel() {
		return funcEbo;
	}

	public void setSysFuncManager(ISysFuncManager funcManager) {
		this.funcManager = funcManager;
	}

	public SysFuncAction() {
		super();
	}

	public String execute() {
		return list();
	}

	public String create() {
		try {
			log.debug(StringUtil.mapObject(funcEbo));
			funcManager.addFunc(funcEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "" + funcEbo.getFID();
			rtnResultMap.put("info", getText(CREATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String update() {
		try {
			log.debug(StringUtil.mapObject(funcEbo));
			funcManager.editFunc(funcEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(UPDATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String delete() {
		try {
			//String delId = request.getParameter("fid");
			//funcManager.deleteFunc(new Integer(delId));
			//System.out.println("----------------------------"+funcEbo.getFID());
			funcManager.deleteFunc(funcEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(DELETE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String list() {
		log.debug("SysFuncAction-list begin");
		try {
			List l = funcManager.getAllFuncList();
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("SysFuncAction-list end");
		return SUCCESS;
	}

	public String test() {
		log.debug("SysFuncAction-test begin");
		try {
			SysFuncsEbo fEbo = new SysFuncsEbo();
			fEbo.setName("testFunc");
			fEbo.setUrl("url");
			fEbo.setDesc("测试功能");
			funcManager.save(fEbo);
			//String delId = request.getParameter("id");
			//funcManager.deleteFunc(new Integer(delId));
			rtnResultMap = new HashMap();
			rtnResultMap.put("info", getText("create.success", "Test Success!"));
			return SUCCESS;
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("SysFuncAction-list end");
		return SUCCESS;
	}

	public String testUpdate() {
		log.debug("SysFuncAction-test begin");
		try {
			SysFuncsEbo fEbo = new SysFuncsEbo();
			fEbo.setFID(1002);
			fEbo.setName("testFunc");
			fEbo.setUrl("url");
			fEbo.setDesc("测试功能222");
			funcManager.save(fEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("info", getText("create.success", "Test Success!"));
			return SUCCESS;
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("SysFuncAction-list end");
		return SUCCESS;
	}	
}